<?php
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['credit_types']=credit_types();
$plugin_name =$this->pluginname;
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	if(!empty($_RQ['code'])){
		$params['code']=trim($_RQ['code']);
		$return['params']['code']=trim($_RQ['code']);
	}
    $fields=array();
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_kabao_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['type']=$this->types;		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'卡包管理'),
			array('op'=>'edit','title'=>'添加卡包'),
		),
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：如果已经添加了卡包，添加卡包批次!','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'groupid','title'=>'分类','type'=>'select','options'=>'groups'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
		),
		'action'=>array(
			array('type'=>'href','action'=>'kabao.batch','title'=>'批次管理'),
		)
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			array('name'=>'code','width'=>15,'title'=>'卡包编号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['groups']=pdo_getall('jy_kabao_group',array('pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kabao_list',array('id'=>$id));
	}else{
		$item=array('isweishop'=>0);
	}
	$item['set']=json_decode($item['set'],true);
	$item['images']=json_decode($item['images'],true);
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'模板设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'卡包管理'),
			array('op'=>'edit','title'=>'添加卡包')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'groupid','title'=>'选择分类','type'=>'select','options'=>'groups'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'卡包价值','type'=>'unitstring','unit'=>'元'),
			array('name'=>'images','title'=>'幻灯片','type'=>'images'),
			array('name'=>'desc','title'=>'卡包描述','type'=>'text'),
			array(
					'type'=>'array',
					'name'=>'set',
					'title'=>'奖品设置',
					'add_tip'=>'添加一个奖项',
						'list'=>array(
							array('type'=>'string','title'=>'奖品名称','name'=>'title'),
							array('type'=>'string','title'=>'奖品描述','name'=>'desc'),
							array('type'=>'radio','title'=>'奖品类型','name'=>'type','help'=>'','options'=>'jiangtype'),
							array('type'=>'string','title'=>'数量','name'=>'num','show'=>array('type'=>array(2,3))),
							array('type'=>'image','title'=>'奖品图片','name'=>'image'),
						),
							'details'=>array(
								array('type'=>'string','title'=>'奖品名称','name'=>'title'),
								array('type'=>'string','title'=>'奖品描述','name'=>'desc'),
								array('type'=>'radio','title'=>'奖品类型','name'=>'type','help'=>'','options'=>'jiangtype'),
								array('name'=>'goodsid','title'=>'送商品','type'=>'choose','help'=>'','action'=>'jy_weishop/goods.get_goods/nomerch=1','show'=>array('type'=>0)),
								array('name'=>'couponid','title'=>'送优惠券','type'=>'choose','help'=>'','action'=>'jy_weishop/coupon.get_coupon//nomerch=1','show'=>array('type'=>1)),
								array('type'=>'radio','title'=>'虚拟币类型','name'=>'credit_type','help'=>'','options'=>'credit_types','show'=>array('type'=>3)),
								array('type'=>'string','title'=>'数量','name'=>'num','show'=>array('type'=>array(2,3))),
								array('type'=>'image','title'=>'奖品图片','name'=>'image','help'=>'温馨提示：<red>建议正方形图片</red>，否则有些地方会显示异常！'),
							)
			),
			array('name'=>'content','title'=>'卡包内容','type'=>'richtext'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['htmls']=$htmls;
	$return['types']=$this->types;
	$return['jiangtype']=$this->jiangtype;
	$return['groups']=pdo_getall('jy_kabao_group',array('pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['groupid']){
		exi('请选择分类！','error');
	}
	$data=array_fields(array('title','price','image','content','groupid','desc','status'),$postdata);
	$data['set']=ijson_encode($postdata['set']);
	$data['images']=ijson_encode($postdata['images']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_kabao_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		$data['type']=0;
		$data['pluginname']=$this->pluginname;
		pdo_insert('jy_kabao_list',$data);
		exi('添加成功！','','list');
	}
}

if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$isadd=pdo_get('jy_kabao_batch',array('listid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('title','id'));	
		if($isadd){
			exi('该卡包已经创建批次，不能删除！','error');
		}
		pdo_delete('jy_kabao_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$isadd=pdo_get('jy_kabao_batch',array('listid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('title','id'));	
				if($isadd){
					exi('该卡包已经创建批次，不能删除！','error');
				}
				pdo_delete('jy_kabao_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

?>